﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_ProductType.cs
* 创建标识：曾乃强 15158756652 2011-04-01 11:04:57
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework.Helper;
using CNVP.Config;

namespace CNVP.JShop.DAL
{
    /// <summary>
    /// 对*CNVP_ProductType*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class CNVP_ProductType
    {
       private string Prefix = DBConfig.Prefix;
        #region DAL
        public int Insert(Model.CNVP_ProductType obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "ProductType ([ProTypeName],[IsEntityType],[IsRefBranks],[IsRefExtendProperty],[IsRefDescripttion],[IsRefShopMustInfo])");
            sql.Append("values(@ProTypeName,@IsEntityType,@IsRefBranks,@IsRefExtendProperty,@IsRefDescripttion,@IsRefShopMustInfo)");
            sql.Append(";SELECT @@IDENTITY");
            SqlParameter[] pra = {
                            new SqlParameter("@ProTypeName",obj.ProTypeName),
							new SqlParameter("@IsEntityType",obj.IsEntityType),
							new SqlParameter("@IsRefBranks",obj.IsRefBranks),
							new SqlParameter("@IsRefExtendProperty",obj.IsRefExtendProperty),
							new SqlParameter("@IsRefDescripttion",obj.IsRefDescripttion),
							new SqlParameter("@IsRefShopMustInfo",obj.IsRefShopMustInfo)
                                 };
            return Convert.ToInt32(DbHelper.ExecuteScalar(sql.ToString(), pra));
        }

        public int Update(Model.CNVP_ProductType obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update " + Prefix + "ProductType set [ProTypeName] = @ProTypeName,[IsEntityType] = @IsEntityType,[IsRefBranks] = @IsRefBranks,[IsRefExtendProperty] = @IsRefExtendProperty,[IsRefDescripttion] = @IsRefDescripttion,[IsRefShopMustInfo] = @IsRefShopMustInfo where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@ProTypeName",obj.ProTypeName),
							new SqlParameter("@IsEntityType",obj.IsEntityType),
							new SqlParameter("@IsRefBranks",obj.IsRefBranks),
							new SqlParameter("@IsRefExtendProperty",obj.IsRefExtendProperty),
							new SqlParameter("@IsRefDescripttion",obj.IsRefDescripttion),
							new SqlParameter("@IsRefShopMustInfo",obj.IsRefShopMustInfo)
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public int Delete(Model.CNVP_ProductType obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from " + Prefix + "ProductType where [ID] = @ID");
            SqlParameter[] pra = {
                                new SqlParameter("@ID",obj.ID)
						
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        public List<Model.CNVP_ProductType> LoadAll()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[ProTypeName],[IsEntityType],[IsRefBranks],[IsRefExtendProperty],[IsRefDescripttion],[IsRefShopMustInfo] from " + Prefix + "ProductType Order by ID");
            DataTable DT = DbHelper.ExecuteTable(sql.ToString());
            List<Model.CNVP_ProductType> list = new List<Model.CNVP_ProductType>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_ProductType cnvp_producttype = new Model.CNVP_ProductType();
                    cnvp_producttype.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    cnvp_producttype.ProTypeName = dr["ProTypeName"] is DBNull ? String.Empty : (string)dr["ProTypeName"];
                    cnvp_producttype.IsEntityType = dr["IsEntityType"] is DBNull ? (byte)0 : (byte)dr["IsEntityType"];
                    cnvp_producttype.IsRefBranks = dr["IsRefBranks"] is DBNull ? (byte)0 : (byte)dr["IsRefBranks"];
                    cnvp_producttype.IsRefExtendProperty = dr["IsRefExtendProperty"] is DBNull ? (byte)0 : (byte)dr["IsRefExtendProperty"];
                    cnvp_producttype.IsRefDescripttion = dr["IsRefDescripttion"] is DBNull ? (byte)0 : (byte)dr["IsRefDescripttion"];
                    cnvp_producttype.IsRefShopMustInfo = dr["IsRefShopMustInfo"] is DBNull ? (byte)0 : (byte)dr["IsRefShopMustInfo"];
                    list.Add(cnvp_producttype);
                }
            }
            return list;

        }

        public Model.CNVP_ProductType LoadById(int ID)
        {

            StringBuilder sql = new StringBuilder();
            sql.Append("select [ProTypeName],[IsEntityType],[IsRefBranks],[IsRefExtendProperty],[IsRefDescripttion],[IsRefShopMustInfo] from " + Prefix + "ProductType");
            sql.Append(" where [ID] = @ID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ID",ID)
						
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(), pra);
            Model.CNVP_ProductType cnvp_producttype = new Model.CNVP_ProductType();
            if (DT != null && DT.Rows.Count > 0)
            {
                DataRow dr = DT.Rows[0];
                cnvp_producttype.ProTypeName = dr["ProTypeName"] is DBNull ? String.Empty : (string)dr["ProTypeName"];
                cnvp_producttype.IsEntityType = dr["IsEntityType"] is DBNull ? (byte)0 : (byte)dr["IsEntityType"];
                cnvp_producttype.IsRefBranks = dr["IsRefBranks"] is DBNull ? (byte)0 : (byte)dr["IsRefBranks"];
                cnvp_producttype.IsRefExtendProperty = dr["IsRefExtendProperty"] is DBNull ? (byte)0 : (byte)dr["IsRefExtendProperty"];
                cnvp_producttype.IsRefDescripttion = dr["IsRefDescripttion"] is DBNull ? (byte)0 : (byte)dr["IsRefDescripttion"];
                cnvp_producttype.IsRefShopMustInfo = dr["IsRefShopMustInfo"] is DBNull ? (byte)0 : (byte)dr["IsRefShopMustInfo"];
            }
            return cnvp_producttype;
        }

        #endregion

        public DataTable GetPage(string PageName, int PageIndex, int PageSize, out int RecordCount, out int PageCount, params SQLConditionInfo[] SqlCondition)
        {
            try
            {
                string AllFields = " ID, ProTypeName, IsEntityType, IsRefBranks, IsRefExtendProperty, IsRefDescripttion, IsRefShopMustInfo";
                string Condition = "" + Prefix + "ProductType";
                string IndexField = "ID";
                string OrderFields = "order by Id Desc";
                return DbHelper.ExecutePage(AllFields, Condition, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount, null);
            }
            catch (Exception e)
            {
                throw new Exception("用于分页的SQL语句无效:" + e.Message);
            }

        }

        /// <summary>
        /// 应用于DropDownList
        /// </summary>
        /// <returns></returns>
        public List<Model.CNVP_ProductType> LoadAllUseDropDownList()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[ProTypeName] from " + Prefix + "ProductType UNION SELECT 0 AS [ID],'--请选择产品类型--' as [ProTypeName] order by [ID] ");
            DataTable DT = DbHelper.ExecuteTable(sql.ToString());
            List<Model.CNVP_ProductType> list = new List<Model.CNVP_ProductType>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_ProductType cnvp_producttype = new Model.CNVP_ProductType();
                    cnvp_producttype.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    cnvp_producttype.ProTypeName = dr["ProTypeName"] is DBNull ? String.Empty : (string)dr["ProTypeName"];
                    list.Add(cnvp_producttype);
                }
            }
            return list;

        }


    }
}

